<?php
/**
 * CreateTime: 2022/4/30 14:38
 */

namespace App\Service\Dictionary;

use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\FormatFunc;
use App\Func\ResponseFunc;
use App\Models\Dictionary\DictionaryModel;
use App\ServiceChecker\Dictionary\DictionaryLegitimacy;
use Illuminate\Http\Exceptions\HttpResponseException;

class DictionaryService
{
    private DictionaryModel $dictionaryModel;
    private DictionaryLegitimacy $dictionaryLegitimacy;

    public function __construct()
    {
        $this->dictionaryModel = new DictionaryModel();
        $this->dictionaryLegitimacy = new DictionaryLegitimacy();
    }

    /**
     * 根据传递过来的字典id返回其下一级子节点
     * @param $dictionaryIds
     * @return array
     */
    public function getDictionaryNextByDictionaryIds($dictionaryIds): array
    {
        $res = $this->dictionaryModel->getDictionaryNextByDictionaryIds($dictionaryIds,[
            "dictionary_id",
            "parent_id",
            "dictionary_name",
            "dictionary_value",
        ])->toArray();
        $parentArr = [];
        $childArr = [];

        foreach($res as $value){
            if(!$value['parent_id']){
                $parentArr[] = $value;
            }else{
                $childArr[$value['parent_id']][] = $value;
            }
        }
        $returnArr = [];
        foreach($parentArr as $key => $value){
            $returnArr[$key] = $value;
            if(isset($childArr[$value['dictionary_id']])){
                $returnArr[$key]['child'] = $childArr[$value['dictionary_id']];
            }
        }
        return $returnArr;
    }

    /**
     * 获取数据字典
     * @return array
     */
    public function getDictionaryAllDictionary(): array
    {

        $dictionary =  $this->dictionaryModel->getDictionaryAllDictionary();
        return FormatFunc::getTreeByData($dictionary->toArray());
    }


    private function checkerAddDictionary($args){
        $this->dictionaryLegitimacy->verifyDictionaryParentIsExist($args['parent_id']);
        $this->dictionaryLegitimacy->verifyDictionaryIsExist(
            $args['dictionary_key']
        );

    }

    public function addDictionary(array $addDictionaryPostData)
    {
        $addDictionaryPostData['parent_id'] = $addDictionaryPostData['parent_id'] ?? 0;
        $this->checkerAddDictionary([
            'dictionary_name' => $addDictionaryPostData['dictionary_name'],
            'parent_id' => $addDictionaryPostData['parent_id'],
            'dictionary_value' => $addDictionaryPostData['dictionary_value'],
            'dictionary_key' => $addDictionaryPostData['dictionary_key'],
        ]);
        if($addDictionaryPostData['dictionary_value'] > 255 || $addDictionaryPostData['dictionary_value'] < 0){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "该字典值错误",
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS,
            ));
        }
        unset($addDictionaryPostData['is_disable']);
        return $this->dictionaryModel->addDictionary($addDictionaryPostData);
    }

    public function editDictionary(array $dictionaryPostData)
    {
        unset($dictionaryPostData['is_disable']);
        unset($dictionaryPostData['dictionary_value']);
        unset($dictionaryPostData['parent_id']);
        unset($dictionaryPostData['dictionary_key']);
        // 校验如果是disable不允许修改
        $canEdit = $this->dictionaryModel->isExistByCondition([
            'dictionary_id' => $dictionaryPostData['dictionary_id'],
            'is_disable' => 1
        ]);

        if($canEdit){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "该字典禁止编辑",
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS,
            ));
        }

        $res =  $this->dictionaryModel->editDictionaryByDictionaryId($dictionaryPostData);
        if(!$res){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "数据字典已编辑或不存在",
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS,
            ));
        }
    }

    public function deleteDictionary($deleteDictionary){
        // 校验如果是disable不允许修改
        $canEdit = $this->dictionaryModel->isExistByCondition([
            'dictionary_id' => $deleteDictionary['dictionary_id'],
            'is_disable' => 1
        ]);

        if($canEdit){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "该字典禁止删除",
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS,
            ));
        }
        $res =  $this->dictionaryModel->deleteDictionary($deleteDictionary);
        if(!$res){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "数据字典已删除或不存在",
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS,
            ));
        }
    }
}